@@ -0,0 +1,56 @@ |
||
1 |
+module Agents |
|
2 |
+ class DropboxFileUrlAgent < Agent |
|
3 |
+ include DropboxConcern |
|
4 |
+ |
|
5 |
+ cannot_be_scheduled! |
|
6 |
+ |
|
7 |
+ description <<-MD |
|
8 |
+ #{'## Include the `dropbox-api` and `omniauth-dropbox` gems in your `Gemfile` and set `DROPBOX_OAUTH_KEY` and `DROPBOX_OAUTH_SECRET` in your environment to use Dropbox Agents.' if dependencies_missing?} |
|
9 |
+ The _DropboxFileUrlAgent_ takes a file path (or multiple files paths) and emits |
|
10 |
+ events with [temporary links](https://www.dropbox.com/developers/core/docs#media). |
|
11 |
+ |
|
12 |
+ The incoming event payload needs to have a `paths` key, with a comma-separated list of files you want the URL for. For example: |
|
13 |
+ |
|
14 |
+ { |
|
15 |
+ "paths": "first/path, second/path" |
|
16 |
+ } |
|
17 |
+ |
|
18 |
+ __TIP__: You can use the _Event Formatting Agent_ to format events before they come in. Here's an example configuration for formatting an event coming out of a _Dropbox Watch Agent_: |
|
19 |
+ |
|
20 |
+ { |
|
21 |
+ "instructions": { |
|
22 |
+ "paths": "{{ added | map: 'path' | join: ',' }}" |
|
23 |
+ }, |
|
24 |
+ "matchers": [], |
|
25 |
+ "mode": "clean" |
|
26 |
+ } |
|
27 |
+ |
|
28 |
+ MD |
|
29 |
+ |
|
30 |
+ event_description <<-MD |
|
31 |
+ The event payload will contain the following fields: |
|
32 |
+ |
|
33 |
+ { |
|
34 |
+ "url": "https://dl.dropboxusercontent.com/1/view/abcdefghijk/example", |
|
35 |
+ "expires": "Fri, 16 Sep 2011 01:01:25 +0000" |
|
36 |
+ } |
|
37 |
+ MD |
|
38 |
+ |
|
39 |
+ def working? |
|
40 |
+ !recent_error_logs? |
|
41 |
+ end |
|
42 |
+ |
|
43 |
+ def receive(events) |
|
44 |
+ events.map { |e| e.payload['paths'].split(',').map(&:strip) } |
|
45 |
+ .flatten.each { |path| create_event payload: url_for(path) } |
|
46 |
+ end |
|
47 |
+ |
|
48 |
+ private |
|
49 |
+ |
|
50 |
+ def url_for(path) |
|
51 |
+ dropbox.find(path).direct_url |
|
52 |
+ end |
|
53 |
+ |
|
54 |
+ end |
|
55 |
+ |
|
56 |
+end |
@@ -0,0 +1,74 @@ |
||
1 |
+require 'spec_helper' |
|
2 |
+ |
|
3 |
+describe Agents::DropboxFileUrlAgent do |
|
4 |
+ before(:each) do |
|
5 |
+ @agent = Agents::DropboxFileUrlAgent.new( |
|
6 |
+ name: 'dropbox file to url', |
|
7 |
+ options: {} |
|
8 |
+ ) |
|
9 |
+ @agent.user = users(:bob) |
|
10 |
+ @agent.service = services(:generic) |
|
11 |
+ @agent.save! |
|
12 |
+ end |
|
13 |
+ |
|
14 |
+ it 'cannot be scheduled' do |
|
15 |
+ expect(@agent.cannot_be_scheduled?).to eq true |
|
16 |
+ end |
|
17 |
+ |
|
18 |
+ it 'has agent description' do |
|
19 |
+ expect(@agent.description).to_not be_nil |
|
20 |
+ end |
|
21 |
+ |
|
22 |
+ it 'has event description' do |
|
23 |
+ expect(@agent.event_description).to_not be_nil |
|
24 |
+ end |
|
25 |
+ |
|
26 |
+ describe "#receive" do |
|
27 |
+ |
|
28 |
+ let(:first_dropbox_url_payload) { { 'url' => 'http://dropbox.com/first/path/url' } } |
|
29 |
+ let(:second_dropbox_url_payload) { { 'url' => 'http://dropbox.com/second/path/url' } } |
|
30 |
+ let(:third_dropbox_url_payload) { { 'url' => 'http://dropbox.com/third/path/url' } } |
|
31 |
+ |
|
32 |
+ def create_event(payload) |
|
33 |
+ event = Event.new(payload: payload) |
|
34 |
+ event.agent = agents(:bob_manual_event_agent) |
|
35 |
+ event.save! |
|
36 |
+ event |
|
37 |
+ end |
|
38 |
+ |
|
39 |
+ before(:each) do |
|
40 |
+ stub.proxy(Dropbox::API::Client).new do |api| |
|
41 |
+ stub(api).find('/first/path') { stub(Dropbox::API::File.new).direct_url { first_dropbox_url_payload } } |
|
42 |
+ stub(api).find('/second/path') { stub(Dropbox::API::File.new).direct_url { second_dropbox_url_payload } } |
|
43 |
+ stub(api).find('/third/path') { stub(Dropbox::API::File.new).direct_url { third_dropbox_url_payload } } |
|
44 |
+ end |
|
45 |
+ end |
|
46 |
+ |
|
47 |
+ context 'with a single path' do |
|
48 |
+ |
|
49 |
+ before(:each) { @event = create_event(paths: '/first/path') } |
|
50 |
+ |
|
51 |
+ it 'creates one event with the temporary dropbox link' do |
|
52 |
+ expect { @agent.receive([@event]) }.to change(Event, :count).by(1) |
|
53 |
+ expect(Event.last.payload).to eq(first_dropbox_url_payload) |
|
54 |
+ end |
|
55 |
+ |
|
56 |
+ end |
|
57 |
+ |
|
58 |
+ context 'with multiple comma-separated paths' do |
|
59 |
+ |
|
60 |
+ before(:each) { @event = create_event(paths: '/first/path, /second/path, /third/path') } |
|
61 |
+ |
|
62 |
+ it 'creates one event with the temporary dropbox link for each path' do |
|
63 |
+ expect { @agent.receive([@event]) }.to change(Event, :count).by(3) |
|
64 |
+ last_events = Event.last(3) |
|
65 |
+ expect(last_events[0].payload).to eq(first_dropbox_url_payload) |
|
66 |
+ expect(last_events[1].payload).to eq(second_dropbox_url_payload) |
|
67 |
+ expect(last_events[2].payload).to eq(third_dropbox_url_payload) |
|
68 |
+ end |
|
69 |
+ |
|
70 |
+ end |
|
71 |
+ |
|
72 |
+ end |
|
73 |
+ |
|
74 |
+end |